#!/usr/bin/perl
# 
# Scott Edwards https://code.google.com/p/tcp-eagain/
# https://www.linkedin.com/profile/view?id=1645831
# I do some contract work on the side. <supadupa@gmail.com>
# 

use warnings;
use strict;
use Data::Dumper;
use Linux::Proc::Net::TCP;

my $s;
my %one;
my %two;

while(1) {
	my $table = Linux::Proc::Net::TCP->read;
	my $needlf = 0;

	for my $entry (@$table) {
		$s=sprintf("%s:%s\t%s:%s",
				$entry->local_address,
				$entry->local_port,
				$entry->rem_address,
				$entry->rem_port,
			  );
		if ($entry->retrnsmt eq 0) {
			delete $one{$s};
			next;
		}
		$one{$s}=0 unless exists $one{$s};
		next if ($entry->retrnsmt eq $one{$s});
		my $t;
		$t = sprintf("%d -> %d\t%s\t%s",
				$one{$s},
				$entry->retrnsmt,
				$s,
				$entry->st,
		);
		$one{$s}=$entry->retrnsmt;
		print "\n".localtime(time())."\n" unless $needlf;
		print "$t\n";
		$needlf++;
	}
	if ($needlf) {
		print Dumper \%one,\%two;
		print "\n";
	}
	sleep 5;
}
